While-loops are usually used when you want to repeat a set of operations until some conditions are met. In this situation, you may not know how many times it will take to fulfil these conditions.
Let's consider an example. We want to demonstrate that
But, we don't know how large
s = 0;
i = 1;
% Arbitrarily large number, so that
% it can enter the while-loop.
err = 100;
% Tolerance.
tol = 1e-6;
while err > tol
s = s + 1/2^i;
i = i + 1;
err = abs(s - 1);
end
i % final i value
err
i =
21.00000000000000
err = 1e-7 ×
9.536743164062500
The above output shows that it took 21 iterations to fulfil the termination condition, which resulted in the error equal to approximately
A while-loop takes the following general form:
while logical_value
statement_1
statement_2
...
statement_n
end
The loop continues until logical_value is false. When a stopping condition is met, this logical_value should evaluate to false and hence the loop terminates. In the example above, the logical_value was given by evaluating the condition err > tol.
The general syntax shown here is the most commonly used form. Nevertheless, a while-loop can also be written in one single line with separators , or ;. Both of the examples below give the same a as the final results.
disp('Example 1')
i = 1;
a = 0;
while a < 5, a = a + i, i = i + 1, end
a
disp('Example 2')
i = 1;
a = 0;
while a < 5; a = a + i; i = i + 1; end
a
Example 1
a =
1.0000
i =
2.0000
a =
3.0000
i =
3.0000
a =
6.0000
i =
4.0000
a =
6.0000
Example 2
a =
6.0000
In the above example, different components of the while-loop are separated by separators. A separator can be a comma, a semicolon, or a newline character.
⚠️⚠️⚠️ The end keyword should be followed by a newline character. This is different from MATLAB in which the end can be followed by other separators.
⚠️⚠️⚠️ If logical_value is always true (e.g., using while true), the while-loop will loop indefinitely without showing any warning or error messages. In this situation, the app may take up so much memory that the user-interface may become unresponsive (the app may even crash). If this happens, try to terminate the execution by tapping the stop button on the top toolbar of the Console. If this doesn't work, try to quite the app entirely.
For example, running the following code will result in infinite looping.
a = 0
while true
a = a + 1;
end
A while-loop can be embedded within another while-loop, as shown in the example below.
sum_all_boxes = 0;
while sum_all_boxes <= 2000
sum_one_box = 0;
while sum_one_box <= 100
sum_one_box = sum_one_box + rand;
end
sum_all_boxes = sum_all_boxes + sum_one_box;
end
sum_all_boxes
sum_all_boxes =
2006.353925543614
There is no do-while loop in both MATLAB and this app. Nevertheless, one can construct a loop similar to do-while by using the keyword break as shown in the example below. It increments a and stops when a is equal to 10. Note here that a is incremented before checking the condition a >= 10. Therefore, it performs like a do-while loop.
a = 0;
while true
a = a + 1;
if a >= 10
break
end
end
a
a =
10.000
a is initially zero. Then, keep adding a random number to a when a is still less than 10. If a is greater than or equal to 10, stop adding. Also, find out how many random numbers have been added.clear
a = 0;
count = 0;
while a < 10
a = a + rand();
count=count + 1;
end
count
All variables cleared
count =
21.000
You have $100 in your pocket. You are going to donate the money to a charity organization following the plan below. Use a while-loop to find out how much money you will donate in total from Day 1 to the last day of donation.
clear
% Initial amount of money
money = 100;
day = 1;
while money >= day
% Donate money.
money = money - day;
day = day + 1;
end
total_donation = 100 - money
All variables cleared
total_donation =
91.000